package com.yzd.learnjava.containers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import com.yzd.learnjava.util.Countries;
import com.yzd.learnjava.util.Print;

public class Lists {
	private static boolean b ;
	private static String s ;
	private static int i ;
	private static Iterator<String> it ;
	private static ListIterator<String> lit;
	
	public static void basicTest(List<String> a) {
		a.add(1,"X");
		a.add("X");
		
		a.addAll(Countries.names(6));
		a.addAll(3, Countries.names(25));
		
		b = a.contains("1");
		b = a.containsAll(Countries.names(25));
		
		s = a.get(1);
		i = a.indexOf("1");
		b = a.isEmpty();
		
		it = a.iterator();
		lit = a.listIterator();
		lit = a.listIterator(3);
		
		i = a.lastIndexOf("1");
		
		a.remove(1);
		a.remove("1");
		
		a.set(1, "y");
		
		a.retainAll(Countries.names(25));
		a.removeAll(Countries.names(25));
		
		i = a.size();
		a.clear();
	}
	
	public static void iterManipulation(List<String> a){
		ListIterator<String> it = a.listIterator();
		it.add("47");
		// must move 
		it.next();
		it.remove();
		//must move
		it.next();
		it.set("47");
	}
	
	public static void iterMotion(List<String> a) {
		ListIterator<String> it = a.listIterator();
		b = it.hasNext();
		b = it.hasPrevious();
		
		s = it.next();
		s = it.previous();
		
		i = it.nextIndex();
		i = it.previousIndex();
	}
	
	
	public static void testVisual(List<String> a) {
		Print.print(a);
		List<String> b = Countries.names(10);
		Print.print("b : " + b);
		a.addAll(b);
		a.addAll(b);
		Print.print("a : " + a);
		
		ListIterator<String> x = a .listIterator(a.size() / 2);
		x.add("one");
		Print.print("a : " + a);
		Print.print("x.next(), " + x.next());
		x.remove();
		Print.print("x.next(), " + x.next());
		x.set("47");
		Print.print("a : " + a);
		
		x = a.listIterator(a.size());
		while(x.hasPrevious()) {
			Print.printnb(x.previous() + " ");
		}
		Print.print();
		Print.print("Visual test finished");
	}
	
	public static void testLinkedList() {
		LinkedList<String> ll = new LinkedList<String>();
		ll.addAll(Countries.names(10));
		Print.print(ll);
		ll.addFirst("One");
		ll.addFirst("Two");
		Print.print(ll);
		Print.print("First: " + ll.getFirst());
		Print.print("remove first, " + ll.removeFirst());
		Print.print("remove first, " + ll.removeFirst());
		Print.print("remove last, " + ll.removeLast());
		Print.print(ll);
	}
	
	public static void main(String[] args) {
		basicTest(new LinkedList<String>(Countries.names(10)));
		basicTest(new ArrayList<String>(Countries.names(10)));
		
		iterMotion(new LinkedList<String>(Countries.names(10)));
		iterMotion(new ArrayList<String>(Countries.names(10)));
		
		iterManipulation(new LinkedList<String>(Countries.names(10)));
		iterManipulation(new ArrayList<String>(Countries.names(10)));
		
		testVisual(new LinkedList<String>(Countries.names(10)));
		
		testLinkedList();
	}
}





















